utils = {}
QMPlugin = utils

function utils.init()
    mlog.info("utils init >>>>>>>>>>>")
end

function utils.unpack(...)
    return zm.TableUnpack(...)
end

function utils.retry(f,count, delay)
    delay = delay or 1000
    count = count or 5
    if not f then
        return false
    end

    local cur = 1
    while cur <= count do
        local rtn1,rtn2,rtn3 = f()
        if rtn1 then
            return rtn1,rtn2,rtn3
        end
        cur = cur +1
        game.delay(delay)
    end

    return false
end

-- r：屏幕坐标/游戏坐标 k：斜率 ox,oy开始坐标 fx,fy目标坐标 offsetX,offsetY偏移
function utils.gameParseScreenZb(r, k, ox, oy, fx, fy, offsetX, offsetY)
    local curX=ox
    local curY=oy
    local tarX=fx
    local tarY=fy

    local rate = r

    local _OOB = tarX - curX
    local _OBP = tarY - curY
    local OB = math.abs(_OOB)
    local BP = math.abs(_OBP)
    local OD = 0
    local DP = 0
    if _OBP == 0 and _OOB == 0 then
        -- 目标坐标=当前坐标
    elseif _OBP * _OOB >= 0 then
        local COA = math.atan(k)
        local OBP = 2 * COA
        local OP = math.sqrt(OB * OB + BP * BP - 2 * BP * OB * math.cos(OBP))
        if OB >= BP then
            local BOPFZ = (OB * OB + OP * OP - BP * BP)
            local BOPFM = (2 * OB * OP)
            local BOPGS = 0
            if BOPFM ~= 0 then
                BOPGS = BOPFZ / BOPFM
            end
            local BOP = math.acos(BOPGS)
            local POD = COA + BOP
            if _OBP >= 0 and _OOB >= 0 then
                OD = -OP * math.cos(POD)
                DP = OP * math.sin(POD)
            else
                OD = OP * math.cos(POD)
                DP = -OP * math.sin(POD)
            end
        else
            local OPBFZ = (OP * OP + BP * BP - OB * OB)
            local OPBFM = (2 * BP * OP)
            local OPBGS = 0
            if OPBFM ~= 0 then
                OPBGS = OPBFZ / OPBFM
            end
            local OPB = math.acos(OPBGS)
            local POD = COA + OPB
            if _OBP >= 0 and _OOB >= 0 then
                OD = OP * math.cos(POD)
                DP = OP * math.sin(POD)
            else
                OD = -OP * math.cos(POD)
                DP = -OP * math.sin(POD)
            end
        end
    elseif _OBP * _OOB <= 0 then
        local BOD = math.atan(k)
        local OBP = 4 * math.atan(1) - 2 * BOD
        local OP = math.sqrt(OB * OB + BP * BP - 2 * BP * OB * math.cos(OBP))
        if OB >= BP then
            local BOPFZ = (OB * OB + OP * OP - BP * BP)
            local BOPFM = (2 * OB * OP)
            local BOPGS = 0
            if BOPFM ~= 0 then
                BOPGS = BOPFZ / BOPFM
            end
            local BOP = math.acos(BOPGS)
            local POD = BOD - BOP
            if _OOB >= 0 and _OBP <= 0 then
                OD = -OP * math.cos(POD)
                DP = OP * math.sin(POD)
            else
                OD = OP * math.cos(POD)
                DP = -OP * math.sin(POD)
            end
        else
            local BOPFZ = (OB * OB + OP * OP - BP * BP)
            local BOPFM = (2 * OB * OP)
            local BOPGS = 0
            if BOPFM ~= 0 then
                BOPGS = BOPFZ / BOPFM
            end
            local BOP = math.acos(BOPGS)
            local POD = BOP - BOD
            if _OOB >= 0 and _OBP <= 0 then
                OD = -OP * math.cos(POD)
                DP = -OP * math.sin(POD)
            else
                OD = OP * math.cos(POD)
                DP = OP * math.sin(POD)
            end
        end
    end

    local moveX = math.floor(OD * rate)
    local moveY = math.floor(DP * rate)

--    mlog.info("moveX=" .. moveX.." moveY=" .. moveY)
    return offsetX + moveX, offsetY + moveY
end



